import { useState } from "react";
import {
  Input,
  Button,
  Card,
  Row,
  Col,
  Statistic,
  // Switch,
  message,
  Divider,
} from "antd";
import {
  CopyOutlined,
  DeleteOutlined,
  ScissorOutlined,
} from "@ant-design/icons";
import { textToolsData } from "@/components/ToolsData";
import ToolTitle from "@/components/ToolTitle";
const { TextArea } = Input;

const TextTrim = () => {
  const [inputText, setInputText] = useState("");
  const [outputText, setOutputText] = useState("");
  const [keepEmptyLines] = useState(true);
  const [trimMode, setTrimMode] = useState<"all" | "start" | "end">("all");
  const [stats, setStats] = useState({ original: 0, trimmed: 0, removed: 0 });

  const processText = () => {
    if (!inputText.trim()) {
      message.warning("请输入需要处理的文本");
      return;
    }

    let removedCount = 0;
    const processedLines = inputText
      .split("\n")
      .map((originalLine) => {
        const originalLength = originalLine.length;
        let processedLine = originalLine;

        // 根据模式处理空格
        if (trimMode === "all" || trimMode === "start") {
          processedLine = processedLine.replace(/^[\s\u3000]+/, "");
        }
        if (trimMode === "all" || trimMode === "end") {
          processedLine = processedLine.replace(/[\s\u3000]+$/, "");
        }

        removedCount += originalLength - processedLine.length;

        // 空行判断使用原始行
        const isEmptyOriginal = originalLine.trim() === "";

        if (isEmptyOriginal && !keepEmptyLines) return null;
        return processedLine;
      })
      .filter(Boolean) as string[];

    setOutputText(processedLines.join("\n"));
    setStats({
      original: inputText.length,
      trimmed: outputText.length,
      removed: removedCount,
    });
  };

  const copyResult = () => {
    if (outputText) {
      navigator.clipboard.writeText(outputText);
      message.success("已复制到剪贴板");
    }
  };

  const clearAll = () => {
    setInputText("");
    setOutputText("");
    setStats({ original: 0, trimmed: 0, removed: 0 });
  };

  return (
    <div className="p-4">
      <Card bordered={false} className="shadow-md">
        <ToolTitle
          title={
            textToolsData.find((item) => item.title === "文本去除两端空格")
              ?.title || ""
          }
          description={
            textToolsData.find((item) => item.title === "文本去除两端空格")
              ?.description || ""
          }
        />
        <Divider />

        <Row gutter={16}>
          <Col span={12}>
            <TextArea
              rows={8}
              value={inputText}
              onChange={(e) => setInputText(e.target.value)}
              placeholder="请输入需要处理的文本..."
              style={{ marginBottom: 16 }}
            />
          </Col>
          <Col span={12}>
            <TextArea
              rows={8}
              value={outputText}
              readOnly
              placeholder="处理结果将显示在这里"
              style={{ marginBottom: 16, backgroundColor: "#fafafa" }}
            />
          </Col>
        </Row>

        <Row gutter={16} className="mb-4">
          <Col>
            <Button
              type="primary"
              onClick={processText}
              icon={<ScissorOutlined />}
            >
              开始处理
            </Button>
          </Col>
          <Col>
            <Button onClick={copyResult} icon={<CopyOutlined />}>
              复制结果
            </Button>
          </Col>
          <Col>
            <Button danger onClick={clearAll} icon={<DeleteOutlined />}>
              清空全部
            </Button>
          </Col>
        </Row>

        <Row gutter={16} className="mb-4">
          {/* <Col>
            <Switch
              checked={keepEmptyLines}
              onChange={setKeepEmptyLines}
              checkedChildren="保留空行"
              unCheckedChildren="删除空行"
            />
          </Col> */}
          <Col>
            <Button.Group>
              <Button
                type={trimMode === "all" ? "primary" : "default"}
                onClick={() => setTrimMode("all")}
              >
                清除两端
              </Button>
              <Button
                type={trimMode === "start" ? "primary" : "default"}
                onClick={() => setTrimMode("start")}
              >
                仅清除开头
              </Button>
              <Button
                type={trimMode === "end" ? "primary" : "default"}
                onClick={() => setTrimMode("end")}
              >
                仅清除末尾
              </Button>
            </Button.Group>
          </Col>
        </Row>

        <Row gutter={16}>
          <Col span={8}>
            <Statistic title="原始字符数" value={stats.original} />
          </Col>
          <Col span={8}>
            <Statistic title="处理后字符数" value={stats.trimmed} />
          </Col>
          <Col span={8}>
            <Statistic title="移除空格数" value={stats.removed} />
          </Col>
        </Row>
      </Card>
    </div>
  );
};

export default TextTrim;
